package handler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"go_mod_test/model"
	"go_mod_test/utils"
	"io/ioutil"
	"net/http"
	"reflect"
)

func HelloHandler(c *gin.Context) {
	//time.Sleep(1*time.Second)
	c.JSON(http.StatusOK, gin.H{
		"message": "Hello vining!",
	})
}

func AddClassHandler(c *gin.Context){
	data, _ := ioutil.ReadAll(c.Request.Body)
	fmt.Println(string(data))
	cls, err := utils.JsonToMap(data)
	err = model.AddMaterielClass(cls)
	if err != nil{
		c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
	}else{
		c.JSON(http.StatusOK, gin.H{"message": "添加成功!"})
	}
}

func GetClassHandler(c *gin.Context) {
	data, _ := ioutil.ReadAll(c.Request.Body)
	data_map, err := utils.JsonToMap(data)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"message": "数据格式错误!"})
		return
	}
	response, err := model.GetMaterielClass(data_map)
	if err != nil {
		fmt.Println(err, reflect.TypeOf(err))
		c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
	}else{
		c.JSON(http.StatusOK, response)
	}

}
